package com.jmu.account;

import java.util.Date;
import java.util.Scanner;

public class CheckingAccount extends Account {

    private int overdraft;
    private String type;

    public CheckingAccount(String id, String name, double annualRate, Date date, int overdraft) {
        super(id, name, annualRate, date);
        this.overdraft = overdraft;
        this.type = "支票账户";
    }

    /**
     * 从余额中取款 （不支持透支行为 透支还需要重写)
     * 1: 取款金额 < 余额+可透支金额  -->  取款成功
     * 2: 取款金额 > 余额+可透支金额  -->  取款失败，提示错误，要求重新输入金额  ...
     * @param amount
     */
    @Override
    public void Withdraw(int amount) {
        //取款金额 < 余额+可透支金额
        if (amount <= (this.balance + this.overdraft)) {
            if (amount <= this.balance) {
                //不需要透支的情况
                this.balance -= amount;
                System.out.println("成功取款￥" + amount);
            } else {
                //需要透支的情况
                int temp = amount - this.balance;
                this.overdraft -= (amount - this.balance);
                this.balance = 0;
                System.out.println("成功取款￥" + amount);
                System.out.println("已透支￥" + temp +
                        " 还可透支￥" + this.overdraft);
            }
        } else {
            //TODO 修改方法

            //取款金额 > 余额
            System.out.println("账户余额不足，仅剩￥" + this.balance +
                    " 且可透支￥" + this.overdraft + " 请重新输入金额");
            Scanner scanner = new Scanner(System.in);
            int newAmount = scanner.nextInt();
            Withdraw(newAmount);
        }
    }

    @Override
    public String toString() {
        return "账户信息: " +
                "可透支金额=" + overdraft +
                ", 账户类型='" + type + '\'' +
                ", 卡号='" + id + '\'' +
                ", 姓名='" + name + '\'' +
                ", 余额=" + balance +
                ", 年利率=" + annualRate +
                ", 开户日期=" + date;
    }
}
